﻿using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Idte.Core.Infrastructure.Repository
{

    public class CodeFirstDbContext : DbContext
    {
        public CodeFirstDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // do not use table name with plurals
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            // Using dynamic model building ref: http://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/
            // Using Fluent API
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                // Get the prototype of DbModelBuilder.Entity<T>() method
                var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");

                // Get all classes with Aggregrate attributes and its subclasses
                var entityTypes = assembly
                  .GetTypes()
                  .Where(t =>
                    t.GetCustomAttributes(typeof(AggregateAttribute), inherit: false)
                    .Any());

                // Add the configuration of each Entity to Model builder
                foreach (var type in entityTypes)
                {
                    entityMethod.MakeGenericMethod(type)
                      .Invoke(modelBuilder, new object[] { });
                }
            }
        }
    }
    public class CodeFirstDbContext<TUser> : IdentityDbContext<TUser> where TUser : IdentityUser
    {

        public CodeFirstDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            //this.Configuration.LazyLoadingEnabled = true;
            //this.Configuration.ProxyCreationEnabled = true;
            //this.Configuration.AutoDetectChangesEnabled = true;
            //this.Configuration.ValidateOnSaveEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            // do not use table name with plurals
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            // Using dynamic model building ref: http://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/
            // Using Fluent API
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                // Get the prototype of DbModelBuilder.Entity<T>() method
                var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");

                // Get all classes with Aggregrate attributes and its subclasses
                var entityTypes = assembly
                  .GetTypes()
                  .Where(t =>
                    t.GetCustomAttributes(typeof(AggregateAttribute), inherit: false)
                    .Any());

                // Add the configuration of each Entity to Model builder
                foreach (var type in entityTypes)
                {
                    entityMethod.MakeGenericMethod(type)
                      .Invoke(modelBuilder, new object[] { });
                }
            }
        }
    }
}
